<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Shop.Classes.DAO
 * @package   PsiFramework.Plugins.Shop.Classes.DAO
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\TaxDAO.html
 */

namespace PsiFramework\Plugins\Shop\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Object\ReferenceObject;


/**
 * The Tax Data Access Object
 *
 * This is the default class for the Tax Data Access Object
 *
 * @category PsiFramework.Plugins.Shop.Classes.DAO
 * @package  PsiFramework.Plugins.Shop.Classes.DAO
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\OrderItemDAO.html
 */
class TaxDAO extends AbstractDAO
{
    /**
     * The singleton
     * 
     * @var TaxDAO
     */
    protected static     $_singleton = null;

    /**
     * Retrieves the tax with its key
     *
     * @param string  $key           The key
     * @param boolean $checkSecurity Check the security ?
     *
     * @return Tax
     */
    public function retrieveByKey(/*string*/ $key, /*boolean*/ $checkSecurity=true)
    {
    	if (isNullOrEmptyString($key)) {
    		return null ;
    	}
    
    	$colNom = new SqlColumn("tax_key");
    
    	$value = mb_strtoupper($key, 'utf-8');
    	$sqlValue = new SqlValue($value);
    
    	$upper = new SqlFunction("UPPER");
    	$upper->addParameter($colNom);
    
    	$op = new SqlOperation(SqlOperation::$equal);
    	$op->addParameter($upper);
    	$op->addParameter($sqlValue);
    
    	$result = $this->retrieveAll(new QueryFilter($op), null, $checkSecurity);
    
    	if (count($result)>0) {
    		return $result[0] ;
    	} else {
    		return null ;
    	}
    }
    
    /**
     * TaxDAO constructor
     * 
     * @param ObjectParameters $objectParameters The object parameters
     * 
     * @return TaxDAO
     */
    protected function __construct(
        ObjectParameters $objectParameters=null
    )
    {
        parent::__construct($objectParameters);
    }
    
    /**
     * The function to call instead of the constructor.
     *
     *@param ObjectParameters $objectParameters the object parameters 
     *                                           for OrderItem
     *
     * @return TaxDAO instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new TaxDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
